package com.qst.rongxiaotong.service.Impl;

import com.alipay.api.internal.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.rongxiaotong.mapper.FinanceMapper;
import com.qst.rongxiaotong.pojo.*;
import com.qst.rongxiaotong.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    private FinanceMapper financeMapper;

    @Override
    public List<Intention> selectIntentionByName(String name) {
        List<Intention> intentionList = financeMapper.selectIntentionByName(name);
        return intentionList;
    }

    @Override
    public void insertIntentionByName(Intention intention) {
        intention.setCreateTime(new Date());
        intention.setUpdateTime(new Date());
        financeMapper.insertIntentionByName(intention);
    }

    @Override
    public void updateIntentionByName(Intention intention) {
        intention.setUpdateTime(new Date());
        financeMapper.updateIntentionByName(intention);
    }

    @Override
    public void deleteIntentionByName(String name) {
        financeMapper.deleteIntentionByName(name);
    }

    @Override
    public List<Recommender> selectRecommend(String name) {
        Intention intention = financeMapper.selectIntention(name);
        if (StringUtils.isEmpty(String.valueOf(intention))) {//无意向
            System.out.println("无意向");
            List<Recommender> list = financeMapper.selectWithNoneIntention(name);
            System.out.println(list);
            return list;
        } else {//有意向
            System.out.println("有意向");
            List<Recommender> list = financeMapper.selectAllRecommend(name);
            System.out.println(list);
            return list;
        }
    }
    @Override
    public PageInfo<Recommender> selectRecommendFY(Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Recommender> orderRecommenders = financeMapper.selectRecommendFY();
        PageInfo<Recommender> pageInfo = new PageInfo<>(orderRecommenders);
        return pageInfo;
    }

    @Override
    public PageInfo<Finance> selectByFinance(Finance finance, Integer pageNum) {
        PageHelper.startPage(pageNum,20);
        List<Finance> finances = financeMapper.selectByFinance(finance);

        PageInfo<Finance> financePageInfo = new PageInfo<>(finances);
        return financePageInfo;
    }

    @Override
    public List<Finance> selectByFinance(Finance finance) {

        List<Finance> finances = financeMapper.selectByFinance(finance);
        return finances;
    }

    @Override
    public Finance selectByFinanceId(Integer id){

        Finance finance = financeMapper.selectByPrimaryKey(id);
        return finance;
    }

    @Override
    public void deleteByFinanceId(Integer id){

        financeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByFinance(Finance finance){

        finance.setUpdateTime(new Date());
        financeMapper.updateByPrimaryKeySelective(finance);

    }

    @Override
    public void addMulti(Finance finance) {
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        finance.setStatus(0);
//        System.out.println("添加融资情报2:"+finance.toString());
        financeMapper.insertMulti(finance);
    }

    @Override
    public void add(Finance finance) {
        finance.setStatus(0);
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        financeMapper.insertSelective(finance);
    }
    @Override
    public FinaceUserDetails selectFinaceUser(String name,String bankId) {
        FinaceUserDetails user = new FinaceUserDetails();
        Bank bank = financeMapper.selectBankById(bankId);
        user.setRate(bank.getRate());
        user.setIntroduce(bank.getIntroduce());
        user.setBankName(bank.getBankName());
        System.out.println("user:"+user.toString());
        return user;
    }

}
